package my.syncfilter;


import my.syncfilter.entity.FileEntity;
import my.syncfilter.entity.SumFile;
import my.syncfilter.ftp.FtpClientTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;


public class FtpUploadFileProcessor extends AbstractFileProcessor {

    private static Logger logger = LoggerFactory.getLogger(FtpUploadFileProcessor.class);

    private final FtpClientTemplate ftpClientTemplate;


    public FtpUploadFileProcessor(String prefix, FileProcessor nextDataTransfer) {
        super(prefix, nextDataTransfer);
        ftpClientTemplate = null;//SpringUtil.getBean(FtpClientTemplate.class);
    }

    @Override
    public boolean process(FileEntity data) {
        SumFile zip = null;
        try {
            zip = data.getZip();
            //如果不上传zip 那么选择普通文件上传
            if (zip == null) {
                zip = data.getSumFile();
            }
            if (zip == null || zip.getFileName() == null) {
                return true;
            }
            //文件已经上传过
            if (zip.isUpload()) {
                return true;
            }
            zip.setUpload(ftpClientTemplate.uploadFile(new File(zip.getBasePath() + zip.getFileName()),
                    billConfig.getUploadDirectory()));
        } catch (Exception e) {
            logger.error("上传数据异常 {} ", zip != null ? zip.getFileName() : "", e);
        }
        return true;
    }
}
